package dgut.bbm.controller;

import java.util.*;
import java.util.concurrent.TimeUnit;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import dgut.bbm.common.exception.BizCodeEnume;
import dgut.bbm.to.FriendTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import dgut.bbm.entity.UserEntity;
import dgut.bbm.service.UserService;
import dgut.bbm.common.utils.PageUtils;
import dgut.bbm.common.utils.R;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

import static dgut.bbm.constant.UserConstant.LOGIN_USER;


/**
 * 
 *
 * @author seventeen
 * @email 1973182107@qq.com
 * @date 2023-05-06 16:56:08
 */
@RestController
@RequestMapping("bbm/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;
    /**
     * 列表
     */


    @RequestMapping("/list")
   // @RequiresPermissions("bbm:user:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/login")
//    public R register(@RequestParam String code, HttpSession session) throws Exception {
    public R register(@RequestParam String code) throws Exception {

        UserEntity userEntity=userService.login(code);
        String wxToken= UUID.randomUUID().toString().replace("-","").substring(20);
//        stringRedisTemplate.opsForValue().set(LOGIN_USER+wxToken,JSON.toJSONString(userEntity),10, TimeUnit.MINUTES);
//
//        session.setAttribute(LOGIN_USER,userEntity);
        if(userEntity==null)
            return R.error(BizCodeEnume.WEIXIN_ID_EXCEPTION.getCode(),BizCodeEnume.WEIXIN_ID_EXCEPTION.getMsg());
        return R.ok().put("toker",wxToken).put("data",userEntity);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
  //  @RequiresPermissions("bbm:user:info")
    public R info(@PathVariable("id") Long id){
		UserEntity user = userService.getById(id);

        return R.ok().put("data", user);
    }

//    @RequestMapping("/info/{wxId}")
//    //  @RequiresPermissions("bbm:user:info")
//    public R info(@PathVariable("wxId") String openId){
//        UserEntity user = userService.getById(openId);
//
//        return R.ok().put("data", user);
//    }
    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("bbm:user:save")
    public R save(@RequestBody UserEntity user){
		userService.save(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("bbm:user:update")
    public R update(@RequestBody UserEntity user){
		userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("bbm:user:delete")
    public R delete(@RequestBody Long[] ids){
		userService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
